﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;

namespace h2100
{
    /// <summary>
    /// Quản lý tất cả các đơn vị (lính/quái) trong game
    /// </summary>
    public class hUnitManager : hEntityManager
    {
        private List<hUnit> _units = new List<hUnit>();
        public List<hUnit> Units
        {
            get { return _units; }
            set { _units = value; }
        }

        private hParams args;

        public hUnitManager(hParams _args)
        {
            args = _args;
        }

        public void initPrototypes(ContentManager content, String fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            String str = "";

            _nprototype = int.Parse(reader.ReadLine()) + 1;
            _prototype = new hUnit[_nprototype];
            for (int i = 1; i < _nprototype; i++)
            {
                hUnit temp = new hUnit();
                temp._nsprite = 1;
                temp._sprite = new hSprite[1];
                temp._sprite[0] = new hSprite(1);
                str = reader.ReadLine();
                temp.Id = int.Parse(str.Split('#')[0]);
                temp.Name = str.Split('#')[1];
                temp.Cost = int.Parse(reader.ReadLine().Split('=')[1]);
                temp.Controller = reader.ReadLine().Split('=')[1];
                temp.Power = int.Parse(reader.ReadLine().Split('=')[1]);
                temp.Energy = int.Parse(reader.ReadLine().Split('=')[1]);
                temp.Actions = actionParse(reader.ReadLine().Split('=')[1].Split(' '));
                temp.Texture = content.Load<Texture2D>(hGlobal.PathUnit + reader.ReadLine().Split('=')[1]);
                temp._sprite[0].Texture = new Texture2D[1];
                temp._sprite[0].Texture[0] = temp.Texture;
                str = reader.ReadLine();
                temp.Width = int.Parse(str.Split('=')[1].Split(' ')[0]);
                temp.Height = int.Parse(str.Split('=')[1].Split(' ')[1]);
                temp.NFrameRow = int.Parse(reader.ReadLine().Split('=')[1]);
                temp.NFrameColumn = int.Parse(reader.ReadLine().Split('=')[1]);
                str = reader.ReadLine();
                temp.FRAppear = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRStand = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();// bỏ một dòng đánh dấu
                str = reader.ReadLine();
                temp.FRMoveUp = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRMoveDown = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRMoveLeft = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRMoveRight = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();// bỏ một dòng đánh dấu
                str = reader.ReadLine();
                temp.FRAttackUp = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRAttackDown = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRAttackLeft = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRAttackRight = new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                str = reader.ReadLine();
                temp.FRDie= new hUnit.FrameRange(int.Parse(str.Split(' ')[1]), int.Parse(str.Split(' ')[2]));
                _prototype[i] = temp;
            }

            reader.Close();
        }

        public hVisibleGameEntity createObject(int idx, float posx, float posy )
        {
            if (idx < 1 || idx >= _nprototype)
                return null;
            hUnit u = (hUnit)((hUnit)_prototype[idx]).clone(args);
            u.X = posx - u.Width/2;
            u.Y = posy - u.Height/2;
            _units.Add(u);
            return _units.Last();
        }

        public void update(hParams args, GameTime time)
        {
            for (int i = 0; i < _units.Count; i++)
            {
                _units[i].update(args, time);
                if (_units[i].IsDead)
                {
                    _units.RemoveAt(i);
                }
                else
                {
                    if (args.ControlPhase != hGlobal.ControlPhase.Selecting)
                    {
                        if (_units[i].Clicked)
                        {
                            args.SelectedUnit = _units[i];
                        }
                        _units[i].Selected = false;
                    }
                }
            }

            if (args.SelectedUnit != null)
            {
                args.SelectedUnit.Selected = true;
                if (args.SelectedStructure != null)
                {
                    args.SelectedStructure.Selected = false;
                    args.SelectedStructure = null;
                }
                if (args.SelectedUnitHasChanged)
                {
                    args.ControlPhase = hGlobal.ControlPhase.Action;
                }
            }
            int countcomunit=0;
            int countplayerunit = 0;
            for (int i = 0; i < _units.Count; i++)
            {
                if (_units[i].Controller.Equals("computer"))
                {
                    countcomunit++;
                }
                if (_units[i].Controller.Equals("player"))
                {
                    countplayerunit++;
                }
            }
            if (countcomunit == 0 || countplayerunit==0)
            {
                args.GameState = hGlobal.GameState.End;
            }
        }

        public void draw(GameTime gtime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _units.Count; i++)
            {
                _units[i].draw(gtime, spriteBatch);
            }
        }

    }
}
